import 'package:flutter/material.dart';

class ClipDemo extends StatelessWidget {

  final avatar = Image.asset('images/icon-40.png', width: 40,);

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('裁剪的例子'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            // 留白
            Container(height: 10,),

            // 不剪裁
            avatar,

            // 留白
            Container(height: 10,),

            //剪裁为圆形
            ClipOval(
              child: avatar,
            ),

            // 留白
            Container(height: 10,),

            //剪裁为圆角矩形
            ClipRRect(
              child: avatar,
              borderRadius: BorderRadius.circular(5),
            ),

            // 留白
            Container(height: 10,),

            // 溢出
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Align(
                  alignment: Alignment.topLeft,
                  widthFactor: 0.5, // 宽度设为原来宽度一半，另一半会溢出
                  child: avatar,
                ),
                Text("你好世界", style: TextStyle(color: Colors.green),),
              ],
            ),

            // 留白
            Container(height: 10,),

            // 裁剪
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ClipRect(
                  child: Align(
                    alignment: Alignment.topLeft,
                    widthFactor: 0.5, // 宽度设为原来宽度一半
                    child: avatar,
                  ),
                ),
                Text("你好世界", style: TextStyle(color: Colors.green),),
              ],
            ),

            // 留白
            Container(height: 10,),

            // 自定义裁剪
            DecoratedBox(
              decoration: BoxDecoration(
                color: Colors.red,
              ),
              child: ClipRect(
                child: avatar,
                clipper: MyClipper(), // 使用自定义的clipper
              ),
            ),

          ],
        ),
      ),
    );
  }
}

class MyClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) => Rect.fromLTWH(5.0, 10.0, 30.0, 20.0);

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) => false;
}